package 堆栈与队列;

import java.util.PriorityQueue;
import java.util.Stack;
import java.util.TreeSet;

public class 有序矩阵中第K小的元素 {
    public static void main(String[] args) {
        有序矩阵中第K小的元素 test = new 有序矩阵中第K小的元素();
        int[][] matrix = {{1, 5, 9}, {10, 11, 13}, {12, 13, 15}};
        test.kthSmallest(matrix, 8);
    }

    public int kthSmallest(int[][] matrix, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
        for (int y = 0; y < matrix.length; y++) {
            for (int x = 0; x < matrix[y].length; x++) {
                int num = matrix[y][x];
                queue.add(num);
            }
        }
        for (int i = 1; i < k; i++) {
            queue.poll();
        }

        return queue.peek();
    }
}
